<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

    public function html()
    {
        header("Content-type: text/html; charset=utf-8");
        $this->load->library('simple_html_dom');
        $html = file_get_html('https://www.google.com.hk/');
        foreach($html->find('a') as $element) {
            echo $element->find('.gbts', 0)->plaintext . PHP_EOL;
        }
    }
	
	public function phpQuery()
	{
        header("Content-type: text/html; charset=utf-8");
        require_once dirname(dirname(__FILE__)) . '/libraries/phpQuery.php';
        //phpQuery::$defaultCharset = 'GB2312';
        phpQuery::newDocumentFile('http://music.baidu.com/');
        $companies = pq('.body .song-list .clearfix');
        foreach($companies as $company)
        {
            $song_name   = trim(pq($company)->find('.song-name a')->text());
            $singer_name = trim(pq($company)->find('.singer-name a')->text());

            if (empty($song_name) || empty($singer_name)) {
                continue;
            }

            $data['info'][] = array('song'=>$song_name, 'singer'=>$singer_name);

        }

        $this->load->view('index', $data);
    }

}
